package com.etheart.framework.web.pager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.etheart.framework.core.dao.Page;


public class Pager<T> {
	private Integer pageMax = 0;
	private String curUrl;
	private List<PageParam> params=new ArrayList<PageParam>(5); 
	private long recordMax = 0;
	private Integer offset = 0;
	
	
	public Pager() {
	}
	public Pager(int pageMax, long recordMax, String curUrl) {
		this.pageMax=pageMax;
		this.recordMax=recordMax;
		this.curUrl=curUrl;
	}
	public Pager(Page<T> page, String curUrl) {
		this.pageMax=page.getPageCnt();
		this.recordMax=page.getRowCnt();
		this.curUrl=curUrl;
	}
	
	public Pager(int offset) {
		this.offset = offset;
	}
	public Integer getPageMax() {
		return pageMax;
	}
	public void setPageMax(Integer pageMax) {
		this.pageMax = pageMax;
	}
	public String getCurUrl() {
		return curUrl;
	}
	public void setCurUrl(String curUrl) {
		this.curUrl = curUrl;
	}
	public List<PageParam> getParams() {
		return params;
	}
	public void setParams(List<PageParam> params) {
		this.params = params;
	}
	public long getRecordMax() {
		return recordMax;
	}
	public void setRecordMax(long recordMax) {
		this.recordMax = recordMax;
	}
	
	@SuppressWarnings("rawtypes")
	public Pager addParam(String name, String value) {
		PageParam pageParamVO=new PageParam();
		pageParamVO.setPname(name);
		pageParamVO.setValue(value);
		params.add(pageParamVO);
		return this;
	}
	public Integer getOffset() {
		return offset+1;
	}
	public void setOffset(Integer offset) {
		this.offset = offset;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Pager upset(Map<String, Object> request, Page page, String curUrl) {
		Pager pager = new Pager(page, curUrl);
		request.put("pager", pager);
		return pager;
	}
	@SuppressWarnings({ "rawtypes" })
	public static Pager upset(Map<String, Object> request, Page page,
			String keyword, String curUrl) {
		return upset(request, page, curUrl).addParam("wd", keyword);
	}
	
}
